package cn.mzcode.xyzpfuwuduan.controller;

import cn.mzcode.xyzpfuwuduan.common.Const;
import cn.mzcode.xyzpfuwuduan.common.ServerResponse;
import cn.mzcode.xyzpfuwuduan.pojo.Student;
import cn.mzcode.xyzpfuwuduan.service.ICollectionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.xml.ws.Action;

@RestController
@RequestMapping("collection")
public class CollectionController {

    @Autowired
    private ICollectionService iCollectionService;


    /**
     * 添加或删除我的收藏
     *
     * @param session
     * @param positionId
     * @return
     */
    @RequestMapping("add_Or_Delete.do")
    public ServerResponse addOrdelete(HttpSession session, Integer positionId) {
        Student student = (Student)session.getAttribute(Const.CURRENT_USER);
        if (student != null) {
            int studentId = student.getId();

            return iCollectionService.addOrdelete(studentId, positionId);
        }
        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户的信息");

    }

    /**
     * 根据用户id获取收藏内容
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("get.do")
    public ServerResponse<PageInfo>  getCollection(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        Student student=(Student)session.getAttribute(Const.CURRENT_USER);
        if (student!=null){
            int studentId=student.getId();
            return iCollectionService.getCollection(studentId,pageNum,pageSize);
        }
        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户的信息");
    }
}
